package com.qikux.vo;

import java.util.List;

/**
 * 专门用来进行分页的一个类
 */

public class Page<T> {
    /**
     * 页码
     */
    private int page = 1;
    /**
     * 每页显示的条数
     */
    private int rows = 10;

    /**
     * 当前页面的数据列表
     */
    private List<T> list;
    /**
     * 总条数
     */
    private int totalRows;

    /**
     * 当前页偏移量
     *
     * @return
     */
    public int getOffset() {
        return (page - 1) * rows;
    }

    /**
     * 总页数
     *
     * @return
     */
    public int getTotal() {
        return (totalRows - 1) / rows + 1;
    }

    /**
     * 是否有下一页
     *
     * @return
     */
    public boolean hasNext() {
        return page < getTotal();
    }

    public boolean hasPrev() {
        return page > 1;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }


    public static PageBuilder builder() {
        return new PageBuilder();
    }


    public static class PageBuilder<T> {

        private Page<T> pageVo = new Page<>();

        public PageBuilder<T> page(int page) {
            pageVo.setPage(page);
            return this;
        }

        public PageBuilder<T> rows(int rows) {
            pageVo.setRows(rows);
            return this;
        }

        public Page<T> build() {
            return pageVo;
        }
    }
}
